<#
Simple PowerShell bench helper:
- starts the app in background (node index.js)
- waits for the server to respond
- runs autocannon via npx
- stops the background process

Usage: in PowerShell run from project root:
  .\bench\run-bench.ps1
#>

Write-Host "Starting server in background..."
$p = Start-Process -FilePath 'node' -ArgumentList 'index.js' -PassThru

Write-Host "Waiting for http://localhost:3000/ to be ready..."
for ($i = 0; $i -lt 20; $i++) {
    try {
        $r = Invoke-WebRequest -UseBasicParsing -Uri 'http://localhost:3000/' -TimeoutSec 1
        if ($r.StatusCode -eq 200) { break }
    } catch { }
    Start-Sleep -Seconds 1
}

if ($i -ge 20) {
    Write-Host "Server did not start in time. Stopping." -ForegroundColor Red
    Stop-Process -Id $p.Id -ErrorAction SilentlyContinue
    exit 1
}

Write-Host "Running built-in Node bench against /api/hello ..."
node .\bench\simple-bench.js -c 50 -d 10 http://localhost:3000/api/hello

Write-Host "Stopping server (pid: $($p.Id))"
Stop-Process -Id $p.Id -ErrorAction SilentlyContinue

Write-Host "Done."
